import requests  # 导入http请求模块
import pytest  # 导入pytest 测试框架
import allure  # 导入allure 测试报告模块
from Common.Random import Pubcs  # 导入封装的随机文字模块
from config import readconfig  # 获取config配置文件信息中 URL、timeout
import time  # 导入时间模块

Conf = readconfig.Read()  # 实例化 readconfig.Read 这个类


@allure.feature("医珍通系统_管理员端_创建医院接口")  # 一级标签
class Test_CreateHospital:
    global YYurl, YYid, YYusername, YYpassword, TM, wz1, wz2, sz, Atime  # 全局变量    下游接口可直接使用
    YYurl = Conf.get_Hospital("URL")  # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")  # 调用配置的医院名称
    YYusername = Conf.get_Hospital("userName")  # 调用配置的账号
    YYpassword = Conf.get_Hospital("passWord")  # 调用配置的密码
    TM = Conf.get_Hospital("timeout")  # 调用配置的超时时间
    wz1 = '李杨'  # 调用封装的随机汉字
    wz2 = '李杨'  # 调用封装的随机汉字
    sz = Pubcs.Suiji()  # 调用封装的随机数字
    Atime = time.strftime('%Y-%m-%d  %H:%M:%S', time.localtime(time.time()))

    @allure.title("医珍通系统_管理员端_上传医院营业执照接口")  # 用例标题
    @pytest.mark.run(order=1)  # 自定义用例执行顺序
    @allure.severity(allure.severity_level.NORMAL)  # 用例严重级别--普通
    @allure.description("医珍通系统_管理员端_上传医院营业执照获取图片ID")  # 用例的详细说明
    def test_A001(self):
        file = {"file": ('营业执照.jpg', open("D:\测试数据\测试图片\营业执照.jpg", "rb"), 'image/jpg')}
        r = requests.post(YYurl + "/yahoo/file/upload/", files=file, timeout=int(TM))
        a = r.json()
        global tid
        tid = a["success"]
        # print()
        # print(a)
        # print(tid)
        if r.status_code == 200:
            assert a["success"] is not None
        else:
            raise RuntimeError("管理员端_上传医院营业执照接口出现异常！！！")

    @allure.title("医珍通系统_管理员端_医院注册申请接口")  # 用例标题
    @pytest.mark.run(order=2)  # 自定义用例执行顺序
    @allure.severity(allure.severity_level.NORMAL)  # 用例严重级别--普通
    @allure.description("医珍通系统_管理员端_医院注册申请")  # 用例的详细说明
    def test_A002(self):
        Canshu = {"hospitalName": wz1 + "医院",
                  "hospitalAdmin": wz1 + "院长",
                  "hospitalPhone": "120" + str(sz),
                  "hospitalAddress": "XX市,XX区,XX区-" + str(sz) + "号",
                  "hospitalImage": tid,
                  "createPeople": wz2,
                  "createPhone": "18888886666"}
        r = requests.post(YYurl + "/yahoo/official/account/onLineCommit", json=Canshu, timeout=int(TM))
        a = r.json()
        global hsnm
        hsnm = wz1 + "医院"
        if r.status_code == 200:
            assert a["count"] == 1
            assert a["state"] == "success"
        else:
            raise RuntimeError("医院注册申请接口出现异常！！！")

    @allure.title("医珍通系统_管理员端_查询所有的医院注册申请信息接口")  # 用例标题
    @pytest.mark.run(order=3)  # 自定义用例执行顺序
    @allure.severity(allure.severity_level.NORMAL)  # 用例严重级别--普通
    @allure.description("医珍通系统_管理员端_查询所有的医院注册申请信息，获取ID")  # 用例的详细说明
    def test_A003(self):
        Canshu = {"name": "",
                  "phone": "",
                  "pageSize": 10,
                  "currentPage": 1,
                  "pageFlag": "N"}
        r = requests.post(YYurl + "/yahoo/official/account/getOnlineData", json=Canshu, timeout=int(TM))
        a = r.json()
        le = a["data"]
        # print()
        # print(a)
        # print(a["data"][0]["id"])

        if r.status_code == 200:
            try:
                for n in range(len(le)):
                    if le[n]["hospitalName"] == hsnm:
                        global hsid
                        hsid = le[n]["id"]
                        # print()
                        # print(hsid)
                assert a["data"] is not None
                assert a["state"] == "success"
                assert a["totalCount"] > 0
                print("\n执行A计划")
            except (AssertionError, IndexError):
                assert a["state"] == "success"
                assert a["totalCount"] == 0
                print("\n执行B计划")
        else:
            raise RuntimeError("查询所有的医院注册申请信息，获取ID接口出现异常！！！")

    @allure.title("医珍通系统_管理员端_创建医院接口")  # 用例标题
    @pytest.mark.run(order=4)  # 自定义用例执行顺序
    @allure.severity(allure.severity_level.NORMAL)  # 用例严重级别--普通
    @allure.description("医珍通系统_管理员端_创建医院")  # 用例的详细说明
    def test_A004(self):
        Canshu = {"hospitalName": wz1 + "医院",
                  "hospitalCarousel": "/uploaded/default/lunbo.png",
                  "address": "XX市,XX区,XX区-" + str(sz) + "号",
                  "directorName": wz1 + "院长",
                  "phone": "120" + str(sz),
                  "password": "82273a6ba2126d3af24ce2d0cb0af515",
                  "twoPassword": "96e79218965eb72c92a549dd5a330112",
                  "qualifications": "/uploaded/" + tid,
                  "onLineId": hsid}
        r = requests.post(YYurl + "/yahoo/hosptital/registerHosptital", json=Canshu, timeout=int(TM))
        a = r.json()
        print()
        print("医院ID：" + a["data"])
        print("\n医院名称：" + wz1 + "医院")
        print("\n一级管理员账号：admin")
        print("\n一级管理员密码：a000000")
        print("\n二级管理员账号：manage")
        print("\n二级管理员密码：111111")
        if r.status_code == 200:
            assert a["data"] is not None
            assert a["success"] == "操作成功"
        else:
            raise RuntimeError("创建医院接口出现异常！！！")
